﻿using Springwater.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Springwater.DataAccessLayer.Repository
{
    public class UnitOfWork : IDisposable
    {
        private SWModelContainer context = new SWModelContainer();
        private BorvizRepository borvizRepository;
        private GenericRepository<Meres> meresRepository;
        private GenericRepository<Vizgyujto> vizgyujtoRepository;
        private GenericRepository<Vizhozam> vizhozamRepository;
        private GenericRepository<Megfigyeles> megfigyelesRepository;
        private GenericRepository<Elnevezes> elnevezesRepository;
        private FenykepRepository fenykepRepository;

        public BorvizRepository BorvizRepository
        {
            get
            {
                if (this.borvizRepository == null)
                {
                    this.borvizRepository = new BorvizRepository(context);
                }

                return borvizRepository;
            }
        }
        public GenericRepository<Meres> MeresRepository
        {
            get
            {
                if (this.meresRepository == null)
                {
                    this.meresRepository = new GenericRepository<Meres>(context);
                }

                return meresRepository;
            }
        }
        public GenericRepository<Vizgyujto> VizgyujtoRepository
        {
            get
            {
                if (this.vizgyujtoRepository == null)
                {
                    this.vizgyujtoRepository = new GenericRepository<Vizgyujto>(context);
                }

                return vizgyujtoRepository;
            }
        }
        public GenericRepository<Vizhozam> VizhozamRepository
        {
            get
            {
                if (this.vizhozamRepository == null)
                {
                    this.vizhozamRepository = new GenericRepository<Vizhozam>(context);
                }

                return vizhozamRepository;
            }
        }
        public GenericRepository<Megfigyeles> MegfigyelesRepository
        {
            get
            {
                if (this.megfigyelesRepository == null)
                {
                    this.megfigyelesRepository = new GenericRepository<Megfigyeles>(context);
                }

                return megfigyelesRepository;
            }
        }
        public GenericRepository<Elnevezes> ElnevezesRepository
        {
            get
            {
                if (this.elnevezesRepository == null)
                {
                    this.elnevezesRepository = new GenericRepository<Elnevezes>(context);
                }

                return elnevezesRepository;
            }
        }
        public FenykepRepository FenykepRepository
        {
            get
            {
                if (this.fenykepRepository == null)
                {
                    this.fenykepRepository = new FenykepRepository(context);
                }

                return fenykepRepository;
            }
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public List<string> SafeSave()
        {            
            try
            {
                context.SaveChanges();
                return null;
            }
            catch (DbEntityValidationException dbEntityValidationException)
            {
                List<string> errorList = new List<string>();

                foreach (var e in dbEntityValidationException.EntityValidationErrors)
                {
                    Borviz borviz = e.Entry.Entity as Borviz;
                    StringBuilder entityValidationError = new StringBuilder();

                    e.ValidationErrors.ToList().ForEach(ve => entityValidationError.AppendFormat("{0}; ", ve.ErrorMessage));
                    errorList.Add(string.Format("Borviz Kod: {0} - Error: {1}", borviz.Kod, entityValidationError.ToString()));
                }

                return errorList;
            }
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
